package org.csu.forum.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.forum.domain.PO.Friend;
import org.csu.forum.domain.PO.InventMessage;
import org.csu.forum.domain.PO.Message;
import org.csu.forum.mapper.FriendMapper;
import org.csu.forum.mapper.InventMessageMapper;
import org.csu.forum.mapper.MessageMapper;
import org.csu.forum.service.CommunicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommunicationServiceImpl implements CommunicationService {

    @Autowired
    FriendMapper friendMapper;

    @Autowired
    MessageMapper messageMapper;

    @Autowired
    InventMessageMapper inventMessageMapper;


    public List<Message> getMessageList(Friend friend) {

        //获取用户对好友发送的信息
        List<Message>messageList1 = new ArrayList<>();
        QueryWrapper<Message>messageQueryWrapper = new QueryWrapper<>();
        messageQueryWrapper.eq("sender_id",friend.getUserid()).eq("receiver_id",friend.getFriendId());
        messageList1 = messageMapper.selectList(messageQueryWrapper);

        //获取好友对用户发送的信息
        List<Message>messageList2 = new ArrayList<>();
        messageQueryWrapper.clear();
        messageQueryWrapper.eq("sender_id",friend.getFriendId()).eq("receiver_id",friend.getUserid());
        messageList2 = messageMapper.selectList(messageQueryWrapper);

        //合并两种信息
        List<Message>messageList = new ArrayList<>();
        messageList.addAll(messageList1);
        messageList.addAll(messageList2);

        return messageList;

    }


    public void insertMessage(Message message) {

        messageMapper.insert(message);

    }

    public void insertInventMessage(InventMessage inventMessage) {

        inventMessageMapper.insert(inventMessage);

    }


    public InventMessage getInventMessage(int inventorid) {

        //获取邀请信息
        InventMessage inventMessage = new InventMessage();
        QueryWrapper<InventMessage>inventMessageQueryWrapper = new QueryWrapper<>();
        inventMessageQueryWrapper.eq("inventor_id",inventorid);
        inventMessage =  inventMessageMapper.selectOne(inventMessageQueryWrapper);

        return inventMessage;
    }

    public void insertFriend(Friend friend) {

        friendMapper.insert(friend);

    }


    public void deleteFriend(Friend friend) {
        QueryWrapper<Friend>friendQueryWrapper = new QueryWrapper<>();
        friendQueryWrapper.eq("userid",friend.getUserid()).eq("friendid",friend.getFriendId());
        friendMapper.delete(friendQueryWrapper);
    }

}
